package com.example.replacebooks.service;

import cn.hutool.core.date.DateUtil;
import com.example.replacebooks.common.JwtTokenUtils;
import com.example.replacebooks.entity.*;
import com.example.replacebooks.mapper.BookorderMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class BookorderService {
    @Resource
    private BookorderMapper bookorderMapper;

    @Resource
    private Orders_BookorderService orders_bookorderService;

    @Resource
    private OrderService orderService;

    public List<Bookorder> findAll(){
        return bookorderMapper.selectAll();
    }

    public PageInfo<Bookorder> findBySearch(Params params){
        User currentUser = JwtTokenUtils.getCurrentUser();
        Integer userid = currentUser.getUserid();
        String name = currentUser.getName();
        Integer role = currentUser.getRole();
        // 开启分页查询
        PageHelper.startPage(params.getPageNum(),params.getPageSize());
        // 接下来的查询会自动按照当前开启的分页查询
        List<Bookorder> list = bookorderMapper.findBySearch(params,userid,name,role);
        return PageInfo.of(list);
    }

    public PageInfo<Bookorder> findByStatuSearch(Params params){
        User currentUser = JwtTokenUtils.getCurrentUser();
        Integer userid = currentUser.getUserid();
        String name = currentUser.getName();
        Integer role = currentUser.getRole();
        // 开启分页查询
        PageHelper.startPage(params.getPageNum(),params.getPageSize());
        // 接下来的查询会自动按照当前开启的分页查询
        List<Bookorder> list = bookorderMapper.findByStatuSearch(params,userid,name,role);
        return PageInfo.of(list);
    }

    public void add(Bookorder bookorder) {
        // 1. 进行重复性判断，同一id的地址替换，不同id新增
        Bookorder bookorderid = bookorderMapper.findByBookOrderid(bookorder.getBookorderid());
        if (bookorderid != null){
            // 该id已存在
            bookorderMapper.updateByBookorderid(bookorder);
        }else{
            Date date = new Date();
            bookorder.setTime(DateUtil.formatDateTime(date));
            bookorder.setNo(DateUtil.format(date,"yyyyMMdd") + System.currentTimeMillis());
            bookorder.setUserid(JwtTokenUtils.getCurrentUser().getUserid());
            List<Orders> orders = bookorder.getOrders();
            // 先创建订单
            bookorderMapper.insertSelective(bookorder);
            // 创建完才能获取到bookorderid
            for (Orders orders1 : orders){
                Orders_Bookorder orders2 = new Orders_Bookorder();
                orders2.setBookid(orders1.getBookid());
                orders2.setNum(orders1.getNum());
                orders2.setBookorderid(bookorder.getBookorderid());
                orders_bookorderService.add(orders2);

                // 删除选中的购物车数据
                orderService.delete(orders1.getOrderid());
            }
        }
    }

    public void delete(Integer bookorderid) {
        bookorderMapper.deleteByPrimaryKey(bookorderid);
    }

    //付款
    public void pay(Bookorder bookorder) {
        if (bookorder.getState().equals("已付款") || bookorder.getState().equals("售后中") || bookorder.getState().equals("已处理")){
        }else {
            bookorder.setState("已付款");
            bookorder.setPaytime(DateUtil.now());
            bookorderMapper.updateByBookorderid(bookorder);
        }
    }

    // 申请售后服务
    public void service(Bookorder bookorder) {
        if (bookorder.getState().equals("未付款") || bookorder.getState().equals("售后中") || bookorder.getState().equals("已处理")){

        }else {
            bookorder.setState("售后中");
            bookorder.setPaytime(DateUtil.now());
            bookorderMapper.updateByBookorderid(bookorder);
        }
    }

    // 售后处理
    public void handle(Bookorder bookorder) {
        bookorder.setState("已处理");
        bookorder.setPaytime(DateUtil.now());
        bookorderMapper.updateByBookorderid(bookorder);
    }
}
